
<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template #ratio_map="{row}">
      <el-descriptions :column="1" border>
        <el-descriptions-item :label="sr.name+'【'+sr.pay_type+'】'" v-for="sr in sharing_receiver_map">
          <el-input v-model="row.value[sr.id]" :name="'ratio['+sr.id+']'" type="number" style="width: 120px;">
            <template #append >%</template>
          </el-input>
        </el-descriptions-item>
      </el-descriptions>
    </template>
  </forms>
</template>

<script>
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, info_param = '', btns = undefined;
    let object = {
      link_id: this.$get('link_id'),
      link_table: this.$get('link_table'),
    }
    // 如果当前操作不是add ，就设定info_param
    if (typeof object.link_id != 'undefined'){
      info_param = `link_table=${object.link_table}&link_id=${object.link_id}`;
    }
    var status_config = _config('status_alias_map.sharing_ratio.status');
    status_config = status_config.slice(0,2);

    let forms_config = {
      object_title: {label:'项目详情',name:'object_title',value:""},
      link_table: {label:'项目类型',name:'link_table',type:'select_one',config:[],tips:'',value:''},
      link_id: {label:'项目ID',name:'link_id',type:'text_one',tips:'如果设置为0，表示该类型的所有项目',config:'',value:0},
      ratio_map: {label:'分账比例',name:'ratio_map',type:'ratio_map',tips:'',value:{}},
      status: {label:'状态',name:'status',type:'radio_switch',config:status_config,tips:'',value:1},
    };
    if (info_param){
      forms_config.link_table.remove = forms_config.link_id.remove = true;
      if (!object.link_table) forms_config.status.remove = true;
    }else {
      forms_config.object_title.remove = true;
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      info_param: info_param,
      btns: btns,
      config: forms_config,
      level_list: [],
      model_map: {},
      sharing_receiver_map: {},
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.info_param ? `/admin/sharing_ratio/edit?`+this.info_param : '/admin/sharing_ratio/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('sharing_ratio/index', '$refs.list_url.list');
          iframe.hide('sharing_ratio/edit');
          if (!this.info_param) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    encode_json(arr){
      let res = {};
      s94Mjs.each(arr, function (v,k){
        if (v != '') res[k] = Number(v);
      })
      return JSON.stringify(res);
    },
    change_kickback(row,lid,v){
      if (v=='') {
        delete row.value[lid];
      }else {
        row.value[lid] = Number(v);
      }
    },
  },
  watch:{
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.info_param) {
      ajax({url:'/admin/sharing_ratio/info?'+this.info_param, cache:true}, (res)=>{
        app.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('sharing_ratio/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/sharing_ratio/config_map',cache:true},function (res){
      app.sharing_receiver_map = res.data.sharing_receiver_map;
      app.config.link_table.config = res.data.link_table_map;
      return false;
    })
  }
}
</script>
